<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="安全服务链名称" prop="sscName">
      <el-input v-model="dataForm.sscName" placeholder="安全服务链名称"></el-input>
    </el-form-item>
    <el-form-item label="起点安全域标识ID" prop="startDomainId">
      <el-input v-model="dataForm.startDomainId" placeholder="起点安全域标识ID"></el-input>
    </el-form-item>
    <el-form-item label="终点安全域标识ID" prop="endDomainId">
      <el-input v-model="dataForm.endDomainId" placeholder="终点安全域标识ID"></el-input>
    </el-form-item>
    <el-form-item label="运行状态(编辑中、运行)" prop="runState">
      <el-input v-model="dataForm.runState" placeholder="运行状态(编辑中、运行)"></el-input>
    </el-form-item>
    <el-form-item label="用户标识" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户标识"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          sscId: 0,
          sscName: '',
          startDomainId: '',
          endDomainId: '',
          runState: '',
          userId: ''
        },
        dataRule: {
          sscName: [
            { required: true, message: '安全服务链名称不能为空', trigger: 'blur' }
          ],
          startDomainId: [
            { required: true, message: '起点安全域标识ID不能为空', trigger: 'blur' }
          ],
          endDomainId: [
            { required: true, message: '终点安全域标识ID不能为空', trigger: 'blur' }
          ],
          runState: [
            { required: true, message: '运行状态(编辑中、运行)不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户标识不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.sscId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.sscId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/securityservicechain/info/${this.dataForm.sscId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sscName = data.securityservicechain.sscName
                this.dataForm.startDomainId = data.securityservicechain.startDomainId
                this.dataForm.endDomainId = data.securityservicechain.endDomainId
                this.dataForm.runState = data.securityservicechain.runState
                this.dataForm.userId = data.securityservicechain.userId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/securityservicechain/${!this.dataForm.sscId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'sscId': this.dataForm.sscId || undefined,
                'sscName': this.dataForm.sscName,
                'startDomainId': this.dataForm.startDomainId,
                'endDomainId': this.dataForm.endDomainId,
                'runState': this.dataForm.runState,
                'userId': this.dataForm.userId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
